package com.cuput.partybuilding.contact.presenter;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

import com.cuput.partybuilding.contact.bean.Contact;
import com.cuput.partybuilding.contact.bean.Group;
import com.cuput.partybuilding.publiclibrary.base.BasePresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wei on 2017-05-08.
 */

public class ContactPresenter extends BasePresenter {
    // 联系人显示名称
    private static final int PHONES_DISPLAY_NAME_INDEX = 0;
    // 电话号码
    private static final int PHONES_NUMBER_INDEX = 1;
    // 联系人的ID
    private static final int PHONES_CONTACT_ID_INDEX = 2;

    // 联系人映射
    private static final String[] PHONES_PROJECTION = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.CONTACT_ID };

    private Context mContext;

    public ContactPresenter(Context context) {
        super(context);
        this.mContext = context;
    }

    /**
     * 获取系统的联系人列表
     * @return 系统联系人
     */
    public ArrayList<Contact> getPhoneContacts() {
        ArrayList<Contact> contacts = new ArrayList<>();

        ContentResolver contentResolver = mContext.getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                Contact contact = new Contact();
                contact.setContactId(cursor.getLong(PHONES_CONTACT_ID_INDEX));
                contact.setPhoneNumber(cursor.getString(PHONES_NUMBER_INDEX));
                contact.setContractName(cursor.getString(PHONES_DISPLAY_NAME_INDEX));
                contacts.add(contact);
            }

            cursor.close();
        }

        return contacts;
    }

    /**
     * 实现分组
     * @param resource
     * @return
     */
    public ArrayList<Group> implGroup(ArrayList<Contact> resource) {
        ArrayList<Group> groups = new ArrayList<>();

        HashMap<String, ArrayList<Contact>> hashMap = new HashMap<>();
        ArrayList<Contact> contacts = null;

        for (int i = 0; i < resource.size(); i++) {
            Contact contact = resource.get(i);

            if (i < 5) {
                contacts = hashMap.get("财务部");
                if (contacts == null) {
                    contacts = new ArrayList<>();
                    hashMap.put("财务部", contacts);
                }
                contacts.add(contact);
            } else if (i < 10) {
                contacts = hashMap.get("技术部");
                if (contacts == null) {
                    contacts = new ArrayList<>();
                    hashMap.put("技术部", contacts);
                }
                contacts.add(contact);
            } else if (i < 15) {
                contacts = hashMap.get("人力部");
                if (contacts == null) {
                    contacts = new ArrayList<>();
                    hashMap.put("人力部", contacts);
                }
                contacts.add(contact);
            } else {
                contacts = hashMap.get("信息部");
                if (contacts == null) {
                    contacts = new ArrayList<>();
                    hashMap.put("信息部", contacts);
                }
                contacts.add(contact);
            }
        }

        for (Map.Entry<String, ArrayList<Contact>> entry : hashMap.entrySet()) {
            Group group = new Group();
            group.setGroupName(entry.getKey());
            group.setContacts(entry.getValue());
            groups.add(group);
        }

        return groups;
    }

    /**
     * 从原数组中找到与搜索字段匹配的结果
     * @param res
     * @param searchText
     * @return 搜索结果
     */
    public ArrayList<Contact> findSearch(ArrayList<Contact> res, String searchText) {
        ArrayList<Contact> contacts = new ArrayList<>();

        for (Contact contact : res) {
            if (contact.getPhoneNumber().contains(searchText)
                    || contact.getContractName().contains(searchText)) {
                contacts.add(contact);
            }
        }

        return contacts;
    }

    @Override
    public void subscribe() {

    }
}
